#include <iostream>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <algorithm>
#include "majestic.h"
using namespace std;

Majestic::Screen::Screen(int w, int h,int xs, int ys)
{

    sf::View camera;
    xsize = xs;
    ysize = ys;
    mtab = new Majestic::Slate *[xs];
    for ( int i = 0; i < xs; ++i ) mtab[i] = new Majestic::Slate[ys];
	width = w;
	height = h;
    App.Create(sf::VideoMode(w, h, 32), "Majestic Engine");
    App.SetFramerateLimit(30);

}
Majestic::Screen::Screen()
{
    sf::View camera;
    xsize = 1;
    ysize = 1;
    mtab = new Majestic::Slate *[1];
    for ( int i = 0; i < 1; ++i ) mtab[i] = new Majestic::Slate[1];
	width = 800;
	height = 600;
	App.Create(sf::VideoMode(width, height, 32), "Majestic Engine");
	App.SetFramerateLimit(30);

}

bool Majestic::Screen::isOpened()
{
    if (App.IsOpened()) return true;
    return false;
}
void Majestic::Screen::activate()
{
    App.SetActive(true);
}
void Majestic::Screen::disactivate()
{
    App.SetActive(false);
}
void Majestic::Screen::addtxt(const char *txt, const char *pfont,int size ,int r, int g, int b, int x, int y)
{
    sf::Font font;
    if (!font.LoadFromFile(pfont)) return;
    sf::Text text(txt, font, size);
    text.SetColor(sf::Color(r,g,b));
    text.SetPosition(x,y);
    App.Draw(text);
}

void Majestic::Screen::setbg(const char *path)
{
   /* string tmp(path);
    char numstr[4];
    char numstr2[4];
    itab = new  sf::Texture *[xsize];
    for ( int i = 0; i < xsize; ++i ) itab[i] = new sf::Texture[ysize];
    stab = new  sf::Sprite *[xsize];
    for ( int i = 0; i < xsize; ++i ) stab[i] = new sf::Sprite[ysize];
    for(int i=0;i<xsize;i++)
    {
        for (int j=0;j<ysize;j++)
        {
            if(!itab[i][j].LoadFromFile(tmp+itoa(i+1,numstr,10)+"x"+itoa(j+1,numstr2,10)+".bmp")) cout  << " not found";
            cout << tmp+itoa(i+1,numstr,10)+"x"+itoa(j+1,numstr2,10)+".bmp"<<endl;
            itab[i][j].SetSmooth(false);
            stab[i][j].SetTexture(itab[i][j]);
            stab[i][j].SetX(i*80);
            stab[i][j].SetY(j*60);
        }
    }*/



}
void Majestic::Screen::drawbg()
{
    /*for(int i=0;i<xsize;i++)
    {
        for (int j=0;j<ysize;j++)
        {
            App.Draw(stab[i][j]);
        }
    }*/
    sf::Texture tmp;
    if(!tmp.LoadFromFile("media/trawa.jpg")) cout  << " not found";
    tmp.SetSmooth(false);
    sf::Sprite smp;
    smp.SetTexture(tmp);
    smp.SetX(0);
    smp.SetY(0);
    App.Draw(smp);
}
void Majestic::Screen::refresh()
{
    App.Display();
}

bool Majestic::Screen::pause(float s)
{

}
void Majestic::Screen::quit()
{
    App.Close();
}
bool Majestic::Screen::isColide(Object o1, Object o2)
{
	if ( o1.gety() - o1.geth() <= o2.gety() && o1.gety() + o1.geth() >= o2.gety() + o1.geth() || o2.gety() - o2.geth() <= o1.gety() && o2.gety() + o2.geth() >= o1.gety() + o2.geth() )
	{
		if (o1.getx() <= o2.getx() + o2.getw() && o1.getx() + o1.getw() >= o2.getx() + o1.getw() )
		{
			return true;
		}
	if (o1.getx() + o1.getw() >= o2.getx() && o1.getx() + o1.getw() <= o2.getx() + o1.getw())
	{
		return true;
	}
	}
	return false;
}
void Majestic::Screen::addslate(int sx,int sy,Slate &s)
{
    mtab[sx][sy] = s;
}
void Majestic::Screen::addobject(int sx,int sy,Object &o1)
{
    mtab[sx][sy].addobject(o1);
}
void Majestic::Screen::drawobjects()
{
    Object tmp;
    list<Object> slatemap;
    std::list<Object>::iterator iter;
    for (int i=0;i<xsize;i++)
    {
        for (int j=0;j<ysize;j++)
        {
    slatemap = mtab[i][j].draw();
    for (iter = slatemap.begin(); iter != slatemap.end(); iter++)
    {

        tmp = *iter;
        App.Draw(tmp.getsprite());
    }
        }
    }

}
void Majestic::Screen::drawplayer()
{
    App.Draw(player->getsprite());
}
void Majestic::Screen::setcamera(int x, int y)
{
    camera.SetViewport(sf::FloatRect(x, y, x+width, y+height));
    App.SetView(camera);
}
void Majestic::Screen::movecamera(int x, int y)
{
    camera.Move(x,y);
}
void Majestic::Screen::zoom(float z)
{
    camera.Zoom(z);
}
void Majestic::Screen::addplayer(Player &p1)
{
    player = &p1;

}
void Majestic::Screen::moveplayer(int x, int y)
{
    player->move(x,y);
}
